package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.UserAffMapper;
import net.edu_soft.ec.asset.pojo.UserAff;
import net.edu_soft.ec.asset.pojo.UserAffExample;
import net.edu_soft.ec.asset.service.UserAffService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.EduCloudsUtils;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserAffServiceImpl implements UserAffService {

    @Autowired
    private UserAffMapper userAffMapper;

    @Override
    public List<String>  getFollows(Integer classes) {
        if(classes<0 || classes >2){
            throw new ParameterException("关注类型参数错误");
        }
        List<String>  ids = new ArrayList<>();
        UserAffExample example = new UserAffExample();
        UserAffExample.Criteria criteria = example.createCriteria();
        //添加状态
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        if(classes==EduConstant.USER_PRODUCT){
            //添加关注的产品id集合不为空
            criteria.andProductIdsIsNotNull();
        }
        if(classes==EduConstant.USER_FACTORY){
            //添加关注的厂家id集合不为空
            criteria.andFactoryIdsIsNotNull();
        }
        if(classes==EduConstant.USER_PRODUCT_TYPE){
            //添加关注的产品类型id集合不为空
            criteria.andProductTypeIdsIsNotNull();
        }
        List<UserAff> userAffList = userAffMapper.selectByExample(example);
        for (UserAff userAff: userAffList) {
            if(classes==EduConstant.USER_PRODUCT){
                //获取该用户关注的产品
                ids.add(userAff.getProductIds());
            }
            if(classes==EduConstant.USER_FACTORY){
                //获取该用户关注的厂家
                ids.add(userAff.getFactoryIds());
            }
            if(classes==EduConstant.USER_PRODUCT_TYPE){
                //获取该用户关注的产品类型
                ids.add(userAff.getProductTypeIds());
            }
        }
        return ids;
    }

    @Override
    public List<UserAff> findByUserInfoId(Integer id){
        UserAffExample example = new UserAffExample();
        UserAffExample.Criteria criteria = example.createCriteria();
        criteria.andUserInfoIdEqualTo(id);
        return  userAffMapper.selectByExample(example);
    }

    @Override
    public long countByExample(UserAffExample example) {
        return 0;
    }

    @Override
    public int deleteByExample(UserAffExample example) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Integer userAffId) {
        return 0;
    }

    @Override
    public int insert(UserAff record) {
        return 0;
    }

    @Override
    public int insertSelective(UserAff record) {
        return 0;
    }

    @Override
    public List<UserAff> selectByExample(UserAffExample example) {
        return userAffMapper.selectByExample(example);
    }

    @Override
    public UserAff selectByPrimaryKey(Integer userAffId) {
        return null;
    }

    @Override
    public int updateByExampleSelective(UserAff record, UserAffExample example) {
        return userAffMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(UserAff record, UserAffExample example) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeySelective(UserAff record) {
        return userAffMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKey(UserAff record) {
        return userAffMapper.updateByPrimaryKey(record);
    }


    @Override
    public UserAff findUserAffByUserId(Integer userId) {
        return selByUserId(userId);
    }

    @Override
    public int attentionOrNo(Integer userId, Integer subjectId, Integer flag, Integer type) {
        PuingUtil.notNullByZero(subjectId,"用户附属：要关注或取消的id不能为空或者小于等于0");
        UserAff userAff =selByUserId(userId);
        //判断是否存在用户id
        List<Integer> ids = new ArrayList<>();
        if(userAff!=null){
            //取消关注
            if(flag ==0){
                String strIds = null;
                if(type==EduConstant.USER_PRODUCT){
                    ids= EduCloudsUtils.delValue(userAff.getProductIds(), subjectId);
                    strIds = userAff.getProductIds();
                    userAff.setProductIds(EduCloudsUtils.convertIds(ids));
                }
                if(type==EduConstant.USER_FACTORY){
                    ids = EduCloudsUtils.delValue(userAff.getFactoryIds(),subjectId);
                    strIds = userAff.getFactoryIds();
                    userAff.setFactoryIds(EduCloudsUtils.convertIds(ids));
                }
                if(type==EduConstant.USER_PRODUCT_TYPE){
                    ids = EduCloudsUtils.delValue(userAff.getProductTypeIds(),subjectId);
                    strIds = userAff.getProductTypeIds();
                    userAff.setProductTypeIds(EduCloudsUtils.convertIds(ids));
                }
                if(strIds!=null&&!"".equals(strIds)){
                    //判断是否删除成功
                    if(ids!=PuingUtil.getNumber(strIds)){
                        //修改关注信息
                        return userAffMapper.updateByPrimaryKeySelective(userAff);
                    }else {
                        throw new OperationException("用户附属:取消关注失败，该id不存在与该用户的关注中");
                    }
                }else {
                    throw new OperationException("用户附属:取消关注失败，该用户没有关注产品或厂家或产品类型");
                }
            }
            //关注
            //存在该用户的附属信息
            //提取类型并判断是否重复添加,重复则抛出异常
            if(type==EduConstant.USER_PRODUCT){
                //比较并添加
                ids = getList(userAff.getProductIds(), subjectId, "用户附属：产品id不能重复添加");
                userAff.setProductIds(EduCloudsUtils.convertIds(ids));
            }
            if(type==EduConstant.USER_FACTORY){
                //比较并添加
                ids = getList(userAff.getFactoryIds(), subjectId, "用户附属：厂家id不能重复添加");
                userAff.setProductIds(EduCloudsUtils.convertIds(ids));
            }
            if(type==EduConstant.USER_PRODUCT_TYPE){
                //比较并添加
                ids = getList(userAff.getProductTypeIds(), subjectId, "用户附属：产品类型id不能重复添加");
                userAff.setProductTypeIds(EduCloudsUtils.convertIds(ids));
            }
            //执行修改操作
            int i = userAffMapper.updateByPrimaryKeySelective(userAff);
            return i;
        }else {
            userAff = new UserAff();
            //不存在就添加，是否添加
            if(type==EduConstant.USER_PRODUCT){
                ids.add(subjectId);
                userAff.setProductIds(EduCloudsUtils.convertIds(ids));
            }
            if(type==EduConstant.USER_FACTORY){
                ids.add(subjectId);
                userAff.setFactoryIds(EduCloudsUtils.convertIds(ids));
            }
            if(type==EduConstant.USER_PRODUCT_TYPE){
                ids.add(subjectId);
                userAff.setProductTypeIds(EduCloudsUtils.convertIds(ids));
            }
            userAff.setUserInfoId(userId);
            userAff.setStatus(EduConstant.STATUS_ZREO);
            //执行添加
            int i = userAffMapper.insertSelective(userAff);
            return i;
        }
    }

    @Override
    public int delByUserId(Integer userId,Integer isDel) {
        //判断是否删除 1为删除
        if (isDel==EduConstant.IS_DEL){
            UserAff userAff = selByUserId(userId);
            if(userAff==null){
                throw new OperationException("用户附属：该用户id查询不到相应信息或已被删除");
            }
            //设置状态为1 不可用
            userAff.setStatus(EduConstant.STATUS_ONE);
            return userAffMapper.updateByPrimaryKeySelective(userAff);
        }else {
            return -1;
        }
    }


    /***
     * 根据用户id查询用户附属信息
     * @param userId 用户id
     * @return 用户附属信息
     */
    private UserAff selByUserId(Integer userId){
        PuingUtil.notNullByZero(userId,"用户附属：用户id不能为空或小于等于0");
        UserAffExample example = new UserAffExample();
        UserAffExample.Criteria criteria = example.createCriteria();
        criteria.andUserInfoIdEqualTo(userId);
        criteria.andStatusEqualTo(EduConstant.STATUS_ZREO);
        List<UserAff> userAffs = userAffMapper.selectByExample(example);
        if(userAffs.size()>0){
            return userAffs.get(0);
        }
        return null;
    }

    /***
     * 提取并比较是否重复
     * @param strIds id串
     * @param id id
     * @param message 错误信息
     * @return 比较后的集合
     */
    private List<Integer> getList(String strIds,Integer id,String message){
        List<Integer> ids = new ArrayList<>();
        if(strIds!=null&&!"".equals(strIds)){
            //提取产品id
            ids = PuingUtil.getNumber(strIds);
            //比较是否重复
            boolean b = EduCloudsUtils.cheakNumbers(ids, id);
            if(b){
                throw new OperationException(message);
            }
            ids.add(id);
        }
        return ids;
    }
}
